package com.uwantsoft.interfaces.web.controller.validator;

import com.uwantsoft.business.service.domain.user.LicensePlate;
import com.uwantsoft.business.service.infrastructure.DriverRepository;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author LeiPeichen.
 */
public class UniqueLicensePlateValidator implements ConstraintValidator<UniqueLicensePlate,String> {

    @Resource(name = "hibernateDriverRepository")
    private DriverRepository driverRepository;

    @Override
    public void initialize(UniqueLicensePlate constraintAnnotation) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        LicensePlate licensePlate = driverRepository.findLicensePlate(value);
        return licensePlate == null;
    }
}
